package com.leetroll.common.utils.exception;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author qdy
 * @Date 2024/4/14 10:08
 */
public class TransformUtil {

    public static String mapToString(Map<String, String[]> map){
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()[0]).append(", ");
        }
        // 移除最后的逗号和空格
        return sb.substring(0, sb.length() - 2);
    }

    /**
     * @methodName：converMap
     * @description：转换request 请求参数
     * @Params： [paramMap]
     * @Return： java.util.Map<java.lang.String, java.lang.String>
     */
    public static Map<String, String> converMap(Map<String, String[]> paramMap) {
        Map<String, String> rtnMap = new HashMap<String, String>();
        for (String key : paramMap.keySet()) {
            rtnMap.put(key, paramMap.get(key)[0]);
        }
        return rtnMap;
    }

    /**
     * @methodName：stackTraceToString
     * @description：转换异常信息为字符串
     * @Params： [exceptionName, exceptionMessage, elements]
     * @Return： java.lang.String
     */
    public static String stackTraceToString(String exceptionName, String exceptionMessage, StackTraceElement[] elements) {
        StringBuilder strbuff = new StringBuilder();
        for (StackTraceElement stet : elements) {
            strbuff.append(stet).append("<br/>");
        }
        return exceptionName + ":" + exceptionMessage + "<br/>" + strbuff.toString();
    }
}
